<?php

namespace app\admin\controller\vip;

use app\common\controller\Backend;

/**
 * 会员卡管理
 */
class Card extends Backend
{
    /**
     * Card模型对象
     * @var object
     * @phpstan-var \app\admin\model\vip\Card
     */
    protected object $model;

    protected string|array $defaultSortField = 'weigh,desc';

    protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];

    protected string|array $quickSearchField = ['id'];

    protected array $withJoinTable = ['level'];


    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\vip\Card();
        $this->request->filter('clean_xss');
    }



    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $data = $this->request->param();
        $map = [];
        if(isset($data['type']) && $data['type']) {
            $map[] = ['type','=',$data['type']];
        }

        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->where($map)
            ->order($order)
            ->paginate($limit);

        foreach ($res as &$val){
            $val['nums'] = $val['use_type'] == 1 ? $val['nums'] : '-';
        }

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */
}
